import 'package:flutter/material.dart';

class AnimatedIconPage extends StatefulWidget {
  const AnimatedIconPage({super.key});

  @override
  State<AnimatedIconPage> createState() => _AnimatedIconPageState();
}

class _AnimatedIconPageState extends State<AnimatedIconPage>
    with SingleTickerProviderStateMixin {
  late AnimationController _controller;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      duration: const Duration(milliseconds: 500),
      vsync: this,
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('AnimatedIcon'),
      ),
      body: Center(
        child: Stack(
          children: [
            ScaleTransition(
              scale: _controller.drive(
                Tween(begin: 0.0, end: 1.0).chain(
                  CurveTween(
                    curve: const Interval(
                      0.5,
                      1.0,
                    ),
                  ),
                ),
              ),
              child: const Icon(
                Icons.close,
                size: 40,
              ),
            ),
            ScaleTransition(
              scale: _controller.drive(
                Tween(
                  begin: 1.0,
                  end: 0.0,
                ).chain(
                  CurveTween(
                    curve: const Interval(
                      0,
                      0.5,
                    ),
                  ),
                ),
              ),
              child: const Icon(
                Icons.search,
                size: 40,
              ),
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          if (_controller.isCompleted) {
            _controller.reverse();
          } else {
            _controller.forward();
          }
        },
        child: const Icon(Icons.play_arrow),
      ),
    );
  }
}
